﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using TYAJZWeb.Data;

namespace TYAJZWeb.Models
{
    public class UserLogonModel
    {
        [Display(Name = "用户名")]
        [Required(ErrorMessage = "用户名必须填写")]
        public String UserName { get; set; }

        [Display(Name = "密码")]
        [Required(ErrorMessage = "密码必须填写")]
        [DataType(DataType.Password)]
        public String PassWord { get; set; }
    }

    public class UserBaseModel
    {

    }

    public enum UserTypes
    {
        Anonymous,
        Viewer,
        Editor,
        Operator,
        Admin
    }

    public class UserListModel : UserBaseModel
    {
        public int Id { get; set; }

        [Display(Name = "真实姓名")]
        public String RealName { get; set; }

        [Display(Name = "用户名")]
        public String UserName { get; set; }
        public UserTypes UserType { get; set; }


        [Display(Name = "用户类型")]
        public String UserTypeString
        {
            get
            {
                switch (UserType)
                {
                    case UserTypes.Admin: return "管理员";
                    case UserTypes.Operator: return "操作员";
                    case UserTypes.Editor: return "录入员";
                    case UserTypes.Viewer: return "访客";
                    default: return string.Empty;
                }
            }
        }

        public static explicit operator UserListModel(User model)
        {
            return new UserListModel()
            {
                Id = model.Id,
                RealName = model.RealName,
                UserName = model.Name,
                UserType = (UserTypes)Enum.Parse(typeof(UserTypes), model.UserType)
            };
        }
    }

    public class UserEditModel : UserBaseModel
    {
        public int Id { get; set; }

        [Display(Name = "真实姓名")]
        [Required]
        public String RealName { get; set; }

        [Display(Name = "用户名")]
        [Required]
        public String UserName { get; set; }

        [Display(Name = "用户类型")]
        [Required]
        public String UserType { get; set; }

        public static explicit operator UserEditModel(User model)
        {
            return new UserEditModel()
            {
                Id = model.Id,
                UserName = model.Name,
                RealName = model.RealName,
                UserType = model.UserType
            };
        }

        public static explicit operator User(UserEditModel model)
        {
            return new User()
            {
                Id = model.Id,
                Name = model.UserName,
                RealName = model.RealName,
                UserType = model.UserType
            };
        }
    }

    public class UserDetailModel : UserListModel
    {
        public static explicit operator UserDetailModel(User model)
        {
            return new UserDetailModel()
            {
                Id = model.Id,
                UserName = model.Name,
                RealName = model.RealName,
                UserType = (UserTypes)Enum.Parse(typeof(UserTypes), model.UserType)
            };
        }
    }

    public class UserChangePasswordModel
    {
        public int Id { get; set; }

        [Display(Name = "真实姓名")]
        public String RealName { get; set; }

        [Display(Name = "新密码")]
        [DataType(DataType.Password)]
        public String Password { get; set; }


        [Display(Name = "确认密码")]
        [DataType(DataType.Password)]
        public String RetypePassword { get; set; }

        public static explicit operator UserChangePasswordModel(User model)
        {
            return new UserChangePasswordModel()
            {
                Id = model.Id,
                RealName = model.RealName
            };
        }
    }
}